#!/usr/bin/perl
#
# take a web page as arg, and replace all text within it with
# the PHP code for outputting a gettext() version of it.
# Based on Gisle Aas' "htextsub" script from the HTML::Parser distro
#
# If this isn't internationalization by hackery, I don't know what
# is...
#
# Note that it needs a hacked HTML::Parser

use strict;

use HTML::Parser;


use HTML::Parser 3.25;
my $p = HTML::Parser->new(unbroken_text => 1,
			  php_mode => 1,
			  default_h => [ sub { print @_; }, "text" ],
                	  text_h    => [ \&edit_print,      "text" ],
        	         );

my $file = shift || usage();
$p->parse_file($file) || die "Can't open file $file: $!\n";

sub usage
{
    my $progname = $0;
    $progname =~ s,^.*/,,;
    die "Usage: $progname <filename>\n";
}

sub edit_print {
    my $string = shift;
    my $accum;
    return if !$string;
    if ($string =~ /^[\s\r\n]+$/) {
	print $string; return;
    }
    if ($string =~ s/^[\s\r\n]+//) {
	print $&;
    }
    if ($string =~ s/[\s\r\n]+$//) {
	$accum = $&;
    }
    $string =~ s/\\/\\/g;
    $string =~ s/\"/\\"/g;
    $string =~ s/\n/ /g;
    print "<?php print _(\"", $string, "\") ?>";
    print $accum;
}











